package me.shurufa.holder;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import butterknife.Bind;
import butterknife.ButterKnife;
import me.shurufa.R;
import me.shurufa.activities.BookDetailActivity;
import me.shurufa.implement.BookLongClickListener;
import me.shurufa.model.Book;
import me.shurufa.utils.Constants;

/**
 * 发现页图书列表ViewHolder
 *
 * @FileName: me.shurufa.holder.DiscoverBookViewHolder.java
 * @author: kHRYSTAL
 * @email: 723526676@qq.com
 * @date: 2016-01-21 18:55
 */
public class DiscoverBookViewHolder extends BaseViewHolder implements View.OnLongClickListener {

    @Bind(R.id.book_img)
    ImageView bookImage;
    @Bind(R.id.book_name)
    TextView bookTitle;

    private Context mContext;
    private Book mBook;
    private int fromWhere;
    private long userId;
    private BookLongClickListener listener;

    public DiscoverBookViewHolder(View itemView,Object parent,int fromWhere,long uid,BookLongClickListener listener){
        this(itemView,parent,fromWhere,uid);
        this.listener = listener;
        if (fromWhere == Constants.TYPE_FROM_MY_BOOKSHELVES)
            itemView.setOnLongClickListener(this);
    }


    public DiscoverBookViewHolder(View itemView,Object parent,int fromWhere,long uid){
        this(itemView,parent);
        this.fromWhere = fromWhere;
        this.userId = uid;
        if (fromWhere == Constants.TYPE_FROM_MY_BOOKSHELVES) {
            bookImage.setOnLongClickListener(this);
            bookTitle.setOnLongClickListener(this);
        }

    }

    public DiscoverBookViewHolder(View itemView, Object parent) {
        super(itemView, parent);
        mContext = itemView.getContext();
        ButterKnife.bind(this, itemView);
        bookTitle.setMaxLines(2);
        addOnClickListener(bookImage);
        addOnClickListener(bookTitle);
    }

    public void bind(Book book) {
        if (book == null)
            return;
        mBook = book;
        if (book.isBookShelvesFavourite){
            bookTitle.setText(mContext.getString(R.string.my_favourite_digest));
            bookImage.setImageResource(R.drawable.ic_favourite);
        }else {

            if (!TextUtils.isEmpty(book.title))
                bookTitle.setText(book.title);
            if (!TextUtils.isEmpty(book.image)) {
                Glide.with(mContext).load(book.image).placeholder(R.drawable.ic_book_loading).error(R.drawable.ic_book_loading).into(bookImage);
            } else {
                bookImage.setImageResource(R.drawable.ic_book_loading);
            }
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        Intent intent = null;
        switch (v.getId()) {
            case R.id.book_img:
            case R.id.book_name:
                switch (fromWhere) {
                    /*case 0:
                        //自定义字段 用来打开我的收藏popupWindow
                        if (mBook.isBookShelvesFavourite) {
                            EventBus.getDefault().post(new Intent(Constants.ACTION_SHELVES_FAVOURITE));
                        } else {
                            intent = new Intent(mContext, AboutBookActivity.class);
                            if (mBook.book_id != 0L) {
                                intent.putExtra(Constants.ARG_BOOK_ID, mBook.book_id);
                            } else {
                                intent.putExtra(Constants.ARG_BOOK_ID, mBook.bid);
                            }
                            LogUtils.kLog().e("bookId: " + mBook.book_id);
                            mContext.startActivity(intent);
                        }
                        break;
                    case Constants.TYPE_FROM_USER_DIGEST_ALL:
                        //传递uid打开目录页获取书的目录与统计目录下相关的书摘
                        intent = new Intent(mContext, CatalogWithDigestActivity.class);
                        intent.putExtra(Constants.ARG_BOOK_ID,mBook.book_id);
                        intent.putExtra(Constants.ARG_USER_ID,userId);
                        mContext.startActivity(intent);
                        break;
                    case Constants.TYPE_FROM_MY_BOOKSHELVES:
                        if (mBook.isBookShelvesFavourite) {
                            EventBus.getDefault().post(new Intent(Constants.ACTION_SHELVES_FAVOURITE));
                        } else {
                            intent = new Intent(mContext, AboutBookActivity.class);
                            intent.putExtra(Constants.ARG_FROM_WHERE,fromWhere);
                            intent.putExtra(Constants.ARG_EXCERPT_BOOKID,mBook.id);
                            if (mBook.book_id != 0L) {
                                intent.putExtra(Constants.ARG_BOOK_ID, mBook.book_id);
                            } else {
                                intent.putExtra(Constants.ARG_BOOK_ID, mBook.bid);
                            }
                            LogUtils.kLog().e("bookId: " + mBook.book_id);
                            mContext.startActivity(intent);
                        }
                        break;
                    case Constants.TYPE_FROM_MY_FAVOURITE:
                        intent = new Intent(mContext, DigestActivity.class);
                        if (mBook.book_id != 0L) {
                            intent.putExtra(Constants.ARG_BOOK_ID, mBook.book_id);
                        } else {
                            intent.putExtra(Constants.ARG_BOOK_ID, mBook.bid);
                        }
                        intent.putExtra(Constants.ARG_FROM_WHERE,fromWhere);
                        mContext.startActivity(intent);
                        break;*/

                    // 跳转至图书详情界面
                    case Constants.TYPE_FROM_INTENT_DETAIL:
                        intent = new Intent(mContext, BookDetailActivity.class);
                        if (mBook.book_id != 0L) {
                            intent.putExtra(Constants.ARG_BOOK_ID, mBook.book_id);
                        } else {
                            intent.putExtra(Constants.ARG_BOOK_ID, mBook.bid);
                        }
                        intent.putExtra(Constants.ARG_FROM_WHERE, fromWhere);
                        mContext.startActivity(intent);
                        break;

                }

                break;
        }
    }

    @Override
    public boolean onLongClick(View v) {
        if (listener!=null && !mBook.isBookShelvesFavourite){
            listener.onBookLongClick(v,mBook);
        }
        return true;
    }
}
